package thread;

/*
  互斥性
  当使用多个synchronized 锁定多个代码片段并且制定的同步监视器对象是同一个时，这些代码片段之间就是互斥的
 */
public class SyncDemo4 {
    public static void main(String[] args) {
        Boo boo = new Boo();
        Thread t1 = new Thread(){
            public void run(){
                boo.A();
            }
        };
        Thread t2 = new Thread(){
            public void run(){
                boo.B();
            }
        };

        t1.start();
        t2.start();
    }
}

class Boo{
    public synchronized void A(){
        Thread t = Thread.currentThread();
        System.out.println(t.getName()+":正在调用A方法...");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(t.getName()+":调用A方法结束！");
    }

    public synchronized void B(){
        Thread t = Thread.currentThread();
        System.out.println(t.getName()+":正在调用B方法...");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(t.getName()+":调用B方法结束！");
    }
}
